package com.ruoyi.shop.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.shop.domain.SysOrders;
import com.ruoyi.shop.service.ISysOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单Controller
 *
 * @author yanghua
 * @date 2020-10-13
 */
@RestController
@RequestMapping("/orders")
public class SysOrdersController extends BaseController {
    @Autowired
    private ISysOrdersService sysOrdersService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:list')")
    @GetMapping("/list")
    @Cacheable(value = "sysOrders22", key = "#sysOrders")
    public TableDataInfo list(SysOrders sysOrders) {
        startPage();
        List<SysOrders> list = sysOrdersService.selectSysOrdersList(sysOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOrders sysOrders) throws IOException {
        List<SysOrders> list = sysOrdersService.selectSysOrdersList(sysOrders);
        ExcelUtil<SysOrders> util = new ExcelUtil<SysOrders>(SysOrders.class);
        util.exportExcel(response, list, "orders");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:query')")
    @GetMapping(value = "/{goodId}")
    public AjaxResult getInfo(@PathVariable("goodId") Long goodId) {
        return AjaxResult.success(sysOrdersService.selectSysOrdersById(goodId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOrders sysOrders) {
        return toAjax(sysOrdersService.insertSysOrders(sysOrders));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOrders sysOrders) {
        return toAjax(sysOrdersService.updateSysOrders(sysOrders));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('shop:orders:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodIds}")
    public AjaxResult remove(@PathVariable Long[] goodIds) {
        return toAjax(sysOrdersService.deleteSysOrdersByIds(goodIds));
    }
}
